function ContourData = MedisContourLoad(ContourFile)

% File to load
% ContourFile  = 'PHT-0001-MedisContour.con';

% Keep track of the time points in the cardiac cycle
CurrentStep = 0;

% Open file, read line by line and parse out needed info.
fid=fopen(ContourFile);
while 1
    
    % Get line of text
    tline = fgetl(fid);
    
    % Check for end of file
    if ~ischar(tline), break, end
    
    % Pixel spacing X
    if strfind(tline,'Pixel_spacing_x')
        ContourData.PixelSpaceX = str2double(tline(strfind(tline,'=')+1:strfind(tline,' mm')-1)); 
    end
    
    % Pixel spacing Y
    if strfind(tline,'Pixel_spacing_y')
        ContourData.PixelSpaceY = str2double(tline(strfind(tline,'=')+1:strfind(tline,' mm')-1)); 
    end
    
    % FOV - x
    if strfind(tline,'Field_of_view_x')
        ContourData.FOVx = str2double(tline(strfind(tline,'=')+1:strfind(tline,' mm')-1)); 
    end
    
    % FOV - y
    if strfind(tline,'Field_of_view_y')
        ContourData.FOVy = str2double(tline(strfind(tline,'=')+1:strfind(tline,' mm')-1)); 
    end
    
    % InterPhase Time Interval
    if strfind(tline,'Interphase delay')
        ContourData.InterPhaseDelay = str2double(tline(strfind(tline,'=')+1:strfind(tline,' ms')-1)); 
    end
    
    % First Delay
    if strfind(tline,'first_delay')
        ContourData.FirstDelay = str2double(tline(strfind(tline,'=')+1:strfind(tline,' ms')-1)); 
    end
    
    % Cardiac Cycle
    if strfind(tline,'Cardiac_cycle')
        ContourData.CardiacCycle = str2double(tline(strfind(tline,'=')+1:strfind(tline,' ms')-1)); 
    end
    
    % zoom
    if strfind(tline,'zoom')
        ContourData.Zoom = str2double(tline(strfind(tline,'=')+1:end)); 
    end
 
    % contrast - Intensity
    if strfind(tline,'display.contrast[MOD]')
        ContourData.Intensity.Contrast = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % brightness - Intensity
    if strfind(tline,'display.brightness[MOD]')
        ContourData.Intensity.Brightness = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % contrast - Phase
    if strfind(tline,'display.contrast[PHASE]')
        ContourData.Phase.Contrast = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % brightness - Phase
    if strfind(tline,'display.brightness[PHASE]')
        ContourData.Phase.Brightness = str2double(tline(strfind(tline,'=')+1:end)); 
    end

    % windowlevelmin - Intensity
    if strfind(tline,'windowlevelmin[MOD]')
        ContourData.Intensity.WinLevMin = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % windowlevelmax - Intensity
    if strfind(tline,'windowlevelmax[MOD]')
        ContourData.Intensity.WinLevMax = str2double(tline(strfind(tline,'=')+1:end)); 
    end

    % windowlevelmin - Phase
    if strfind(tline,'windowlevelmin[PHASE]')
        ContourData.Phase.WinLevMin = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % windowlevelmax - Phase
    if strfind(tline,'windowlevelmax[PHASE]')
        ContourData.Phase.WinLevMax = str2double(tline(strfind(tline,'=')+1:end)); 
    end

    % Center - x
    if strfind(tline,'display.centerx')
        ContourData.Centerx = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % Center - y
    if strfind(tline,'display.centery')
        ContourData.Centery = str2double(tline(strfind(tline,'=')+1:end)); 
    end
    
    % Contours
    if strfind(tline,'[FCONTOUR]')
        
        % Increment cylce time step.
        CurrentStep = CurrentStep + 1;
        
        % Read 1st line of contour - Identifiy information
        tline = fgetl(fid);
        SpaceDelimiters = strfind(tline,' ');
        ContourData.Curve(CurrentStep).Slice     = str2double(tline(1:SpaceDelimiters(1)));
        ContourData.Curve(CurrentStep).Phase     = str2double(tline(SpaceDelimiters(1):SpaceDelimiters(2)));
        ContourData.Curve(CurrentStep).ContourID = str2double(tline(SpaceDelimiters(2):SpaceDelimiters(3)));
        ContourData.Curve(CurrentStep).Zoom      = str2double(tline(SpaceDelimiters(3):end));
        
        % Read 2nd line of contour - Total contour points
        tline = fgetl(fid);
        ContourData.Curve(CurrentStep).TotalContourPoints = str2double(tline);
        
        % Read 3rd line of contour - contour starting point
        tline = fgetl(fid);
        SpaceDelimiters = strfind(tline,' ');
        ContourData.Curve(CurrentStep).StartX = str2double(tline(1:SpaceDelimiters))+4;
        ContourData.Curve(CurrentStep).StartY = str2double(tline(SpaceDelimiters:end))+4;
        
        %%%%% HACK!!!!!
        ContourData.Curve(CurrentStep).StartX = ContourData.Curve(CurrentStep).StartX/2;
        ContourData.Curve(CurrentStep).StartY = ContourData.Curve(CurrentStep).StartY/2;
        
        % Read 4th line of contour - Freeman chain code.
        tline = fgetl(fid);
        length(tline);
        ContourData.Curve(CurrentStep).ChainCode = tline;
 
        % Transform chain code into X-Y pairs (units?? - Assuem pixels)
        ChainLength = length(ContourData.Curve(CurrentStep).ChainCode);
            
        % Error check        
        if ChainLength ~= ContourData.Curve(CurrentStep).TotalContourPoints
           disp('Medis Load: Chain length error');
           return;
        end
       
        %Set first point
        ContourData.Curve(CurrentStep).Contour_U = zeros(ChainLength,1);
        ContourData.Curve(CurrentStep).Contour_V = zeros(ChainLength,1);
        ContourData.Curve(CurrentStep).Contour_U(1) = ...
            ContourData.Curve(CurrentStep).StartX;
        ContourData.Curve(CurrentStep).Contour_V(1) = ...
            ContourData.Curve(CurrentStep).StartY;
        
        for CurrentPoint = 2:1:ChainLength
            
            % Transfor chain codes into X-Y pairs
            switch ContourData.Curve(CurrentStep).ChainCode(CurrentPoint)
                
                % Move right
                case '0'
                ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) + .5;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) + 0;
                    
                % Move right and down    
                case '1'
                ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) + .5;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) + .5;               
                    
                % Move down
                case '2'    
                ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) + 0;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) + .5;                
                    
                % Move left and down    
                case '3'
                ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) - .5;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) + .5;                    
                    
                % move left
                case '4'
                 ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) - .5;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) + 0;                   
                    
                % Move left and up    
                case '5' 
                  ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) - .5;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) - .5;                  
                    
                % Move up    
                case '6' 
                   ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) + 0;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) - .5;                 
                    
                % Move left and up
                case '7'
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) + .5;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) - .5;                
                    
                % Move left and up
                case '8'
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_U(CurrentPoint-1) + 0;
                ContourData.Curve(CurrentStep).Contour_V(CurrentPoint) = ...
                    ContourData.Curve(CurrentStep).Contour_V(CurrentPoint-1) + 0;
                
                % Error    
                otherwise
                    disp('Medis Load: Chain movement error');
                    return;    
            % switch
            end
        
        %Chain points
        end
        
        % Transform and scale UV coordinates into physical units.
        ContourData.Curve(CurrentStep).Contour_X = ...
            (double(ContourData.Curve(CurrentStep).Contour_U)  / ...
             ContourData.Curve(CurrentStep).Zoom             ) * ...
            ContourData.PixelSpaceX;
        
        ContourData.Curve(CurrentStep).Contour_Y = ...
            (double(ContourData.Curve(CurrentStep).Contour_V)  / ...
             ContourData.Curve(CurrentStep).Zoom             ) * ...
            ContourData.PixelSpaceY;
       
    % [FCONTOUR]    
    end    
end
fclose(fid);